$(function () {
    var showTopicListUrl = '/backstage/topic/listByCondition';

    //页码
    var pageIndex = 1;
    //定义一页显示10行数据
    var pageSize = 10;
    //总用户数
    var maxItem = 999;
    //总页数
    var totalPage = 999;

    //搜索的关键字
    var username = '';
    var topicName = '';
    var topicCategoryId = 0;


    showTopicList('','','',pageIndex,pageSize);


    //根据相关条件显示用户信息
    function showTopicList(username,topicName,topicCategoryId,pageIndex,pageSize) {
        $('#username').val(username);
        $('#topicName').val(topicName);
        $('select #topicCategory').find('option[value="'+topicCategoryId+'"]').attr('selected','selected');//选中value="3"的option
        var url = showTopicListUrl + '?deleted=0&head=1&pageIndex='+pageIndex+'&pageSize='+pageSize+'&username='+username+
            '&topicName='+topicName+'&topicCategoryId='+topicCategoryId;
        $.ajax({
            url:url,
            type:'get',
            contentType:false,
            processData:false,
            cache:false,
            beforeSend: function(request){
                request.setRequestHeader("Token",localStorage.getItem('token'));
            },
            success: function (data) {
                if (data.success) {
                    totalPage = data.data.pages;
                    maxItem = data.data.total;
                    var topicList = '';
                    data.data.records.map(function (item,index) {
                        var role = '';
                        var status = '';
                        if (item.personInfo.role=='0'){
                            role = '普通用户'
                        }else if (item.personInfo.role == '1') {
                            role = '超级管理员'
                        }else if (item.personInfo.role == '2') {
                            role = '用户管理员'
                        }else if (item.personInfo.role == '3') {
                            role = '动态管理员'
                        }
                        if (item.enableStatus == '1') {
                            status = '启用'
                        }else if (item.enableStatus == '0') {
                            status = '停用'
                        }
                        var classStatus = '';
                        if (item.enableStatus=='0'){
                            classStatus = 'layui-btn-disabled';
                        }
                        topicList += '<tr><td><input type="checkbox" name="id" value="'+item.topicId+'" lay-skin="primary"></td>'+
                            '<td>'+item.topicId+'</td><td>'+item.topicName+'</td><td>'+item.topicCategoryName+'</td>'+
                            '<td>'+item.personInfo.username+'</td><td>'+role+'</td><td>'+new Date(item.createTime).Format("yyyy-MM-dd")+
                            '</td><td>'+item.priority+'</td><td class="td-status"><span class="layui-btn layui-btn-normal layui-btn-mini '+classStatus+'">'+status+'</span></td>'+
                            '<td class="td-manage"><a onclick="member_stop(this,\''+item.topicId+'\')" href="javascript:;"  title="'+status+'">'+
                            '<i class="layui-icon">&#xe601;</i></a>'+'<a title="修改优先级" onclick="xadmin.open(\'修改\',\'/admin/topic/edit-topic-priority?topicId='+item.topicId+'&priority='+item.priority+'\',600,400)" href="javascript:;">'+
                            '<i class="layui-icon">&#xe642;</i></a>'+'<a title="详情"  onclick="xadmin.open(\'详情\',\'/topic/topicContent?topicId='+item.topicId+'\',700,700)" href="javascript:;">'+
                            '<i class="iconfont">&#xe6a2;</i></a>'+ '</a><a title="删除" onclick="member_del(this,\''+item.topicId+'\')" href="javascript:;">'+
                            '<i class="layui-icon">&#xe640;</i></a></td></tr>';
                    });
                    $('#topicList').html(topicList);
                    $('#current').text(pageIndex);
                    $('.end').text(totalPage);
                }else{
                    if (data.code == 403) {
                        //权限不够
                        $('.x-nav').html('<h1 style="color: red">'+data.message+'</h1>');
                        $('.layui-fluid').hide();
                    }
                }

            }
        })
    }

    //跳转到指定页码
    $('#forward').click(function () {
        var page = $('#page').val();
        if (page) {
            if (page < 0 || page > totalPage) {
                alert("填写的页码不规范");
            }else {
                pageIndex = page;
                showTopicList(username,topicName,topicCategoryId,pageIndex,pageSize);
                $('#page').val('');
            }
        }else {
            alert("填写的页码为空");
        }

    });



    //搜索相关关键字
    $('#searchOrder').click(function () {
        username = $('#username').val();
        topicName = $('#topicName').val();
        topicCategoryId = $('#topicCategory').val();
        showTopicList(username,topicName,topicCategoryId,pageIndex,pageSize);
    });

    //到最后一页
    $('.page .end').click(function () {
        var page = $(this).text();
        pageIndex = page;
        showTopicList(username,topicName,topicCategoryId,pageIndex,pageSize);
    });
    //前一页
    $('.layui-card-body .page .prev').click(function () {
        //如果当前页数为1则不能往前一页
        if (pageIndex>1){
            pageIndex = pageIndex-1;
            showTopicList(username,topicName,topicCategoryId,pageIndex,pageSize);
        }
    });
    //往后一页
    $('.page .next').click(function () {
        //如果当前页数为最后一页则不能往后一页
        if (pageIndex < totalPage) {
            pageIndex = pageIndex + 1;
            showTopicList(username,topicName,topicCategoryId,pageIndex,pageSize);
        }
    });


});
